package pharao.client.views.game.navigation;

import com.google.gwt.user.client.History;

import hmvc4gwt.client.component.IComponent;
import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.framework.client.controller.NavigationController;
import hmvc4gwt.framework.client.events.CloseViewEvent;
import hmvc4gwt.framework.client.events.TerminateEvent;
import pharao.client.dto.UserDTO;
import pharao.client.views.game.landkarte.LandkarteController;
import pharao.client.views.game.landkarte.LandkarteModel;
import pharao.client.views.game.landkarte.LandkarteView;
import pharao.client.views.game.landkarte.events.ShowLandkarteViewEvent;
import pharao.client.views.game.login.events.SetLoginUserEvent;
import pharao.client.views.game.logout.LogoutController;
import pharao.client.views.game.logout.LogoutView;
import pharao.client.views.game.logout.events.ShowLogoutViewEvent;
import pharao.client.views.game.menu.GameMenuController;
import pharao.client.views.game.menu.GameMenuView;
import pharao.client.views.game.menu.events.ShowGameMenuViewEvent;
import pharao.client.views.game.navigation.events.LogoutEvent;
import pharao.client.views.game.navigation.events.ShowGameMenuEvent;
import pharao.client.views.game.player.PlayerController;
import pharao.client.views.game.player.PlayerModel;
import pharao.client.views.game.player.events.GetUserEvent;
import pharao.client.views.game.player.events.SetUserEvent;
import pharao.client.views.navigation.PharaoNavigationModel;

public class GameNavigationController extends NavigationController {
	
	/**
	 * enthält den eingeloggten User
	 */
	private UserDTO loginUser;

	/**
	 * enthält die PlayerComponent
	 */
	private IComponent componentPlayer;
	
	public GameNavigationController(UserDTO loginUser) {
		setLoginUser(loginUser);
	}
	

	public void initImpl() {
		register(ShowGameMenuEvent.class, this);
		
		/*
		 * PlayerController hinzufügen
		 */
		setComponentPlayer(addChildComponent(new PlayerController(), new PlayerModel(getLoginUser())));
		
		register(GetUserEvent.class, this);
		register(LogoutEvent.class, this);
		register(GetUserEvent.class, this);
		register(SetUserEvent.class, this);
	}

	public void handleEventImpl(IHmvcEvent event) {
		
		if (event instanceof ShowGameMenuEvent) {
			showGameMenu();
		} else if ((event instanceof SetLoginUserEvent) || (event instanceof GetUserEvent)) {
			/*
			 * Event an den PlayerController senden
			 */
			//TODO event nur an den PlayerController senden
			fireEvent(event, false, false, true);
		} else if (event instanceof LogoutEvent) {
			/*
			 * Spiel beenden
			 */
			History.newItem(PharaoNavigationModel.linkIndex);
			fireEvent(new TerminateEvent(), true, true, true);
		} else if (event instanceof GetUserEvent) {
			componentPlayer.fireEvent(event, false, true, false);
		} else if (event instanceof SetUserEvent) {
			fireEvent(event, false, false, true);
		}
	}

	public void onHistoryChanged(String historyToken) {
		
		if (historyToken.equals(GameNavigationModel.linkMap)) {
			
			fireEvent(new CloseViewEvent(), false, true, true);
			
			/*
			 * Landkarte anzeigen
			 */
			showLandkarte();
			
		} else if (historyToken.equals(GameNavigationModel.linkGameHelp)) {
			
			fireEvent(new CloseViewEvent(), false, true, true);
			
			/*
			 * Spiel-Hilfe anzeigen
			 */
			showHelp();
			
		} else if (historyToken.equals(GameNavigationModel.linkGameMenu)) {
			
			fireEvent(new CloseViewEvent(), false, true, true);
			
			showGameMenu();
			
		} else if (historyToken.equals(GameNavigationModel.linkStory)) {
			
			fireEvent(new CloseViewEvent(), false, true, true);
			
			showStory();
			
		} else if (historyToken.equals(PharaoNavigationModel.linkLogout)) {
			
			fireEvent(new CloseViewEvent(), false, true, true);
			
			/*
			 * Logout anzeigen
			 */
			addChildComponent(new LogoutController(), new LogoutView());
			fireEvent(new ShowLogoutViewEvent(), false, false, true);
			
		}
		
	}

	private void showGameMenu() {
		addChildComponent(new GameMenuController(), new GameMenuView());
		fireEvent(new ShowGameMenuViewEvent(), false, false, true);
	}

	private void showHelp() {
		
	}

	private void showLandkarte() {
		addChildComponent(new LandkarteController(), new LandkarteModel(), new LandkarteView());
		fireEvent(new ShowLandkarteViewEvent(), false, false, true);
	}

	private void showStory() {
		
	}

	public Class getClassOfObject() {
		return GameNavigationController.class;
	}

	public void terminate() {
		// TODO Auto-generated method stub
		
	}


	private UserDTO getLoginUser() {
		return loginUser;
	}


	private void setLoginUser(UserDTO loginUser) {
		this.loginUser = loginUser;
	}


	private void setComponentPlayer(IComponent componentPlayer) {
		this.componentPlayer = componentPlayer;
	}

}
